#define DEBUG
#include <cstdio>
#define INF 0x7ffffff
#define MAXN 1000
#define MAXB 20
#define MAXNODE (MAXN+MAXB+2)
#define MAXSIDE (MAXN+MAXN*MAXB+MAXB)

using namespace std;

int min(int a, int b) {
  return a<b ? a : b;
}

class Network {
public:
  int n, m, ind[MAXNODE+1], las[MAXNODE+1], link[2*MAXSIDE+2], to[2*MAXSIDE+2], val[2*MAXSIDE+1], dis[MAXNODE+1], gap[MAXNODE+1];
  bool flag;

  void init(int n) {
    this->n = n;
    flag=false;
    m=1;
    gap[0] = n;
    for (int i=1; i<=n; i++) {
      dis[i]=las[i]=ind[i]=0;
    }
  }

  void addSide(int x, int y, int v) {
    m++;
    to[m]=y;
    val[m]=v;
    link[m]=ind[x];
    las[x]=ind[x]=m;
  }

  int runFlow(int s, int t, int flow) {
    if (s==t) {
      return flow;
    }
    int have=0;
    for (int i=las[s]; i; i=link[i]) {
      if (dis[s]==dis[to[i]]+1 && val[i]) {
	las[s]=i;
	int now=runFlow(to[i], t, min(flow-have, val[i]));
	val[i]-=now, val[i^1]+=now;
	have+=now;
	if (have==flow || flag) {
	  return have;
	}
      }
    }
    las[s]=ind[s];
    if (!(--gap[dis[s]])) {
      flag=true;
    }
    gap[++dis[s]]++;
    return have;
  }
};

bool check(int n, int b, int cow[][MAXB+1], int barn[], int first, int last) {
  static Network net;
  net.init(n+b+2);
  for (int i=1; i<=n; i++) {
    net.addSide(1, i+2, 1);
    net.addSide(i+2, 1, 0);
    for (int j=first; j<=last; j++) {
      net.addSide(i+2, cow[i][j]+n+2, 1);
      net.addSide(cow[i][j]+n+2, i+2, 0);
    }
  }
  for (int j=1; j<=b; j++) {
    net.addSide(j+n+2, 2, barn[j]);
    net.addSide(2, j+n+2, 0);
  }

  int ret=0;
  for (; !net.flag; ret+=net.runFlow(1, 2, INF));
  return ret>=n;
}

int main() {
#ifdef DEBUG
  freopen("1259.in", "r", stdin);
  freopen("1259.out", "w", stdout);
#endif

  static int cow[MAXN+1][MAXB+1], barn[MAXB+1];
  int n, b;
  scanf("%d %d", &n, &b);
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=b; j++) {
      scanf("%d", cow[i]+j);
    }
  }
  for (int i=1; i<=n; i++) {
    scanf("%d", barn+i);
  }

  #ifdef DEBUG
  bool flag=check(n, b, cow, barn, 1, b);
  #endif
  int ans=b;
  for (int i=1; i<=b; i++) {
    for (int j=i; j<=b; j++) {
      if (check(n, b, cow, barn, i, j)) {
	ans = min(ans, j-i+1);
      }
    }
  }
  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
